import usePermissionStore from '@/store/permission'
import {CENTER_MENU, EXTRA_AUTH_ROUTE} from '../layout-config'
import { RouteLocationNormalized } from 'vue-router'

export type AuthRouteData = {
  key: string
  web_path: string
  children?: AuthRouteData[]
}

/**
 * 将请求过来的数据拉平, 只取叶子节点
 * @param data 权限中心接口获取的原始数据
 **/
export const parseAuthRouteData = (data:AuthRouteData[]) => {
  const result:AuthRouteData[] = []
  const fn = (nodes:AuthRouteData[]) => {
    nodes.forEach(item => {
      if (!item.children) {
        result.push(item)
      } else {
        fn(item.children)
      }
    })
  }
  fn(data)
  return result
}

/**
 * 根据权限数据重组系统配置按钮
 * @param permiseRoute 
 * @param cfgs 
 */
const _parseMenu = (permiseRoute: AuthRouteData[], cfgs: RouteMenuItem[]) => {
  const unPermise: string[] = []
  const contain:RouteMenuItem[] = []
  /**存储树形链路是否有叶子节点 */
  const treeLinks:any = {}
  const fn = (nodes: RouteMenuItem[], con: RouteMenuItem[], links:string = '') => {
    nodes.forEach(el => {
      const obj = {
        ...el
      }
      delete obj.children
      if (el.children) {
        obj.children = [] as RouteMenuItem[]
        con.push(obj)
        fn(el.children, obj.children, links + '&*#' + el.key)
      } else {
        obj.isLeaf = true
        // 判断是否有路由权限
        if (permiseRoute.some(r => r.key === el.key)) {
          treeLinks[links + '&*#' + el.key] = true
          con.push(obj)
        } else { // 不在权限中配置中，即无权限路由
          unPermise.push(obj.name as string)
        }
      }
    })
  }
  fn(cfgs, contain)
  // 根据菜单配置内是否含有叶子节点，废弃当前节点树
  const set = new Set<string>()
  for (const key in treeLinks) {
    const arr = key.split('&*#').filter(el => el !== '')
    arr.forEach(el => {
      set.add(el)
    })
  }
  const nodeKeys: string[] = Array.from(set)
  const rCon: RouteMenuItem[] = []
  const fn2 = (nodes: RouteMenuItem[], con: RouteMenuItem[]) => {
    nodes.forEach(node => {
      const obj = {
        ...node
      }
      delete obj.children
      if (nodeKeys.includes(node.key)) {
        con.push(obj)
        if (node.children) {
          obj.children = []
          fn2(node.children, obj.children)
        }
      }
    })
  }
  fn2(contain, rCon)
  return {
    menu: rCon,
    unPermiseName: unPermise
  }
}

/**
 * 根据权限数据，格式化布局菜单
 * @param data 权限中心接口获取的原始数据
 * */
export const parseLayoutMenu = (data: AuthRouteData[]) => {
  const permissionStore = usePermissionStore()
  // 格式化权限中心有用数据
  const parsData = parseAuthRouteData(data)
  /**无权限路由存储 */
  const center = _parseMenu(parsData, CENTER_MENU)
  const extra = _parseMenu(parsData, EXTRA_AUTH_ROUTE)
  // 无权限路由name集合
  const unPermise = [...center.unPermiseName, ...extra.unPermiseName]
  permissionStore.headCenterMenu = center.menu
  permissionStore.unPermiseNames = unPermise
}

/**
 * 判断路由跳转是否有权限
 * @param to
 * @returns
 */
export const hasPermission = (to: RouteLocationNormalized) => {
  const permissionStore = usePermissionStore()
  if (to.name) {
    return !permissionStore.unPermiseNames.includes(to.name as string)
  }
  return true
}

/**
 * 获取全部菜单按钮
 */
export const getAllLayoutMenu = () => {
  const permissionStore = usePermissionStore()
  permissionStore.headCenterMenu = CENTER_MENU
}